import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Home from '../components/Home.vue'
import Salary from '../components/user/salary.vue'
import Welcome from '../components/Welcome.vue'
import ItemCat from '../components/items/ItemCat.vue'
import Repertory from '../components/items/repertory.vue'
import AddItem from '../components/items/addItem.vue'
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/elementUI',component: ElementUI},
  {path: '/home',component: Home,redirect: '/welcome',
  children:[
      {path: '/welcome', component: Welcome},
      {path: '/salary', component: Salary},
      {path: '/repertory', component: Repertory},
     // {path: '/item/addItem', component: AddItem},
     // {path: '/itemCat', component: ItemCat}
  ]}
]

const router = new VueRouter({
  routes
})
//设定路由导航守卫
//参数说明：1.to 要访问的路径 2.from 从那个路径来 3.next请求放行
//策略： 1.用户访问/login 放行请求
//       2.如果用户没有访问登录页 则校验用户是否登录。校验token
//         有token  应该放行
//         没有token  应该跳转到登录页面
// router.beforeEach((to,from,next) =>{
//   if(to.path === '/login') return next()
//   //用户访问的网址不是/login 获取 token
//   let token = window.sessionStorage.getItem("token")
//   // if(token != null && token.length > 0)
//   if(token) return next()
//   //如果程序执行到这一行 说明token没数据，跳转到登录页面
//   next("/login")

// })


// 对外声明路由对象 子组件向父组件传递数据
export default router
